
#pragma once

#include <assert.h>

template<typename T>
class transaction_guard
{
public:
    transaction_guard(T& d)
        : db(d)
        , commited(false)
    {
        db.auto_commit(false);
    }

    ~transaction_guard()
    {
        if (!commited) {
            db.roll_back();
        }
        db.auto_commit(true);
    }

    bool commit()
    {
        assert(commited == false);
        commited = db.commit();
        return commited;
    }
private:
    bool        commited;
    T&          db;
};